<?php

/**
 * This is the model class for table "Produit".
 *
 * The followings are the available columns in table 'Produit':
 * @property integer $id
 * @property integer $prix
 * @property string $nom
 * @property string $description
 * @property double $poids
 * @property string $dimension
 * @property integer $quantite
 * @property double $fdp
 * @property integer $id_artiste
 * @property string $urlImage
 * @property integer $id_categorie
 * @property integer $id_type
 *
 * The followings are the available model relations:
 * @property Commande[] $ligneCommandes
 * @property Artiste $artiste
 * @property Categorie $categorie
 * @property Type $type
 */
class Produit extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Produit the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Produit';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
		array('prix, nom, description, poids, dimension, quantite, fdp, urlImage,id_artiste,id_categorie, id_type', 'required'),
		array('prix, quantite, id_artiste, id_categorie, id_type', 'numerical', 'integerOnly'=>true),
		array('poids, fdp', 'numerical'),
		array('nom', 'length', 'max'=>100),
		array('description', 'length', 'max'=>300),
		array('dimension', 'length', 'max'=>30),
		// The following rule is used by search().
		// Please remove those attributes that should not be searched.
		array('id, prix, nom, description, poids, dimension, quantite, fdp, id_artiste, urlImage, id_categorie, id_type', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'ligneCommandes' => array(self::MANY_MANY, 'Commande', 'LigneCommande(id_produit, id_commande)'),
			'artiste' => array(self::BELONGS_TO, 'Artiste', 'id_artiste'),
			'categorie' => array(self::BELONGS_TO, 'Categorie', 'id_categorie'),
			'type' => array(self::BELONGS_TO, 'Type', 'id_type'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'prix' => 'Prix',
			'nom' => 'Nom',
			'description' => 'Description',
			'poids' => 'Poids',
			'dimension' => 'Dimension',
			'quantite' => 'Quantité',
			'fdp' => 'Frais de port',
			'id_artiste' => 'Id Artiste',
			'urlImage' => 'Image',
			'id_categorie' => 'Catégorie',
			'id_type' => 'Type',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('prix',$this->prix);
		$criteria->compare('nom',$this->nom,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('poids',$this->poids);
		$criteria->compare('dimension',$this->dimension,true);
		$criteria->compare('quantite',$this->quantite);
		$criteria->compare('fdp',$this->fdp);
		$criteria->compare('id_artiste',$this->id_artiste);
		$criteria->compare('urlImage',$this->urlImage,true);
		$criteria->compare('id_categorie',$this->id_categorie);
		$criteria->compare('id_type',$this->id_type);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}

	public static function getDixDerniersProduits () {
		$criteria = new CDbCriteria;
		$criteria->limit = 10;
		return Produit::model()->findAll($criteria);
	}
}